package com.hongdee.insurance.task.service.impl;

import com.hongdee.common.util.StringUtil;
import com.hongdee.insurance.task.dao.ITInsuranceTaskSeekHelpDao;
import com.hongdee.insurance.task.service.ITInsuranceTaskSeekHelpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class TInsuranceTaskSeekHelpServiceImpl implements ITInsuranceTaskSeekHelpService {

    @Autowired
    private ITInsuranceTaskSeekHelpDao seekHelpDao;


    /**
     * 求助信息分页查询
     * @param uid
     * @param start
     * @param limit
     * @param data
     * @return
     */
    @Override
    public Map queryPage(String uid, String start, String limit, Map<String, Object> data) throws Exception {
        StringBuffer sql=new StringBuffer();
        List<Object> params=new ArrayList<>();
        sql.append(" select h.*,o.o_name from T_CRM_CAR_task_help h,t_bc_organ o where h.company_id=o.id ");
        if(!"".equals(StringUtil.formatEmpty(data.get("area")))){
            sql.append(" and o.o_area=? ");
            params.add(StringUtil.formatEmpty(data.get("area")));
        }
        if(!"".equals(StringUtil.formatEmpty(data.get("companyId")))){
            sql.append(" and h.company_id=? ");
            params.add(StringUtil.formatEmpty(data.get("companyId")));
        }
        if(!"".equals(StringUtil.formatEmpty(data.get("appealName")))){
            sql.append(" and h.seek_help_user_name like ? ");
            params.add("%"+StringUtil.formatEmpty(data.get("appealName"))+"%");
        }
        if(!"".equals(StringUtil.formatEmpty(data.get("plateNum")))){
            sql.append(" and h.plate_num like ? ");
            params.add("%"+StringUtil.formatEmpty(data.get("plateNum"))+"%");
        }
        if(!"".equals(StringUtil.formatEmpty(data.get("vin")))){
            sql.append(" and h.vin like ? ");
            params.add("%"+StringUtil.formatEmpty(data.get("vin"))+"%");
        }
        if(!"".equals(StringUtil.formatEmpty(data.get("helpDate")))){
            sql.append(" and DATE_FORMAT(h.seek_help_time,'%Y-%m-%d')=DATE_FORMAT(?,'%Y-%m-%d') ");
            params.add(StringUtil.formatEmpty(data.get("helpDate")));
        }
        if(!"".equals(StringUtil.formatEmpty(data.get("doneDate")))){
            sql.append(" and DATE_FORMAT(h.completion_time,'%Y-%m-%d')=DATE_FORMAT(?,'%Y-%m-%d') ");
            params.add(StringUtil.formatEmpty(data.get("doneDate")));
        }
        if(!"".equals(StringUtil.formatEmpty(data.get("status")))){
            sql.append(" and h.status=? ");
            params.add(StringUtil.formatEmpty(data.get("status")));
        }
        return this.seekHelpDao.page(sql.toString(),params.toArray(),start,limit);
    }
}
